package com.dj.springcloud.controller;


import com.dj.springcloud.entities.CommonResult;
import com.dj.springcloud.entities.Payment;
import com.dj.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j

/**
 * @author Dev_J
 * @date 2020-12-10 23:01
 */
public class PaymentControllor {
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private  String serverPort;



    /**
     *  浏览器不支持POST请求，POST使用POSTman
     *  http://127.0.0.1:8001/payment/create/?serial=test200
     * @RequestBody不加会导致其他服务互相范围数据丢失
     * @param payment
     * @return
     */
    @PostMapping(value = "/payment/create")
    public CommonResult create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("插入结果"+ result);
        if (result>0){
            return  new CommonResult(200,"插入数据库成功"+serverPort,result);

        }
        return new CommonResult(404,"错误",null);
    }



    /**
     *  GET请求自测成功
     *  http://127.0.0.1:8001/payment/getPaymentById/1
     * @param id
     * @return
     */
    @GetMapping(value = "/payment/getPaymentById/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id){
        Payment payment = paymentService.getPaymentById(id);
        log.info("查询成功"+ payment.toString());
        int age=8/2;
        if (payment!=null){
            return  new CommonResult(200,"获取数据库成功"+serverPort,payment);

        }
        return new CommonResult(404,"错误",null);
    }


}
